package com.app.knock.db;

import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import z.lib.base.CommonAndroid;
import z.lib.base.LogUtils;
import z.lib.base.BaseUtils;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class Account extends SkypeTable {
	public Account(Context context) {
		super(context);
		addColumns(id);
		addColumns(name);
		addColumns(user_name);
		addColumns(token);
		addColumns(area);
		addColumns(notification_active);
		addColumns(status_late_login);
		addColumns(avatar);
		addColumns(avatar_id);
		addColumns(code);
	}

	public static final String code = "code";
	public static final String id = "id";
	public static final String area = "area";
	public static final String name = "name";
	public static final String user_name = "user_name";
	public static final String notification_active = "notification_active";
	public static final String avatar = "avatar";
	public static final String avatar_id = "avatar_id";
	public static final String token = "token";
	public static final String status_late_login = "status_late_login";

	public void updateWhenLogin(int responseCode, Context context, String response, Map<String, String> params) {
		if (!CommonAndroid.isBlank(response)) {
			try {
				JSONObject mainJsonObject = new JSONObject(response);
				String is_succes = CommonAndroid.getString(mainJsonObject, BaseUtils.KEY.is_success);
				String remember = params.get(BaseUtils.KEY.REMEMBERPASSWORD);

				if (BaseUtils.VALUE.STATUS_API_SUCCESS.equals(is_succes)) {
					JSONObject jsonObject_data = mainJsonObject.getJSONObject("data");
					JSONObject jsonObject = mainJsonObject.getJSONObject("data").getJSONObject("user");
					// all
					ContentValues xValues = new ContentValues();
					xValues.put(status_late_login, "0");
					context.getContentResolver().update(getContentUri(), xValues, null, null);

					ContentValues values = new ContentValues();
					values.put(id, CommonAndroid.getString(jsonObject, id));
					values.put(token, CommonAndroid.getString(jsonObject_data, token));
					values.put(name, CommonAndroid.getString(jsonObject, name));
					values.put(area, CommonAndroid.getString(jsonObject, area));
					values.put(notification_active, CommonAndroid.getString(jsonObject, notification_active));
					values.put(avatar, CommonAndroid.getString(jsonObject, avatar));
					values.put(avatar_id, CommonAndroid.getString(jsonObject, avatar_id));
					values.put(user_name, params.get("user_name"));
					values.put(status_late_login, "1");

					String where = String.format("%s = '%s'", name, params.get("name"));

//					values.put(contract_status, CommonAndroid.getString(jsonObject, contract_status));
//
//					try {
//						JSONObject contract_statusJs = jsonObject.getJSONObject(contract_status);
//						values.put(code, CommonAndroid.getString(contract_statusJs, code));
//						values.put(redirect, CommonAndroid.getString(contract_statusJs, redirect));
//					} catch (Exception exception) {
//
//					}
					if (new Account(context).has(where)) {
						LogUtils.e("datalogin", "update");
						context.getContentResolver().update(getContentUri(), values, where, null);
					} else {
						LogUtils.e("datalogin", "save");
						context.getContentResolver().insert(getContentUri(), values);
					}
				}
			} catch (JSONException e) {
			}
		}
	}

	@Override
	public int getIndex() {
		return 1;
	}

	public Cursor querryLoginAccount() {
		return querry(String.format("%s =='1'", status_late_login));
	}

	public String getUserId() {
		String suser_id = "";
		try {
			Cursor cursor = querryLoginAccount();
			if (cursor != null) {
				if (cursor.moveToNext()) {
					suser_id = CommonAndroid.getString(cursor, id);
				}
				cursor.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return suser_id;
	}
	
	public String getUserName() {
		String suser_id = "";
		try {
			Cursor cursor = querryLoginAccount();
			if (cursor != null) {
				if (cursor.moveToNext()) {
					suser_id = CommonAndroid.getString(cursor, name);
				}
				cursor.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return suser_id;
	}

	public String getToken() {
		String suser_id = "";
		try {
			Cursor cursor = querryLoginAccount();
			if (cursor != null) {
				if (cursor.moveToNext()) {
					suser_id = CommonAndroid.getString(cursor, token);
				}
				cursor.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return suser_id;
	}

}
